class ProductsController < ApplicationController
  before_filter :load_pcate,:only=>[:index,:edit,:update,:destroy,:new,:show,:create]
  before_filter :login_required,:only=>[:favorite,:create_from_flash]
  before_filter :find_user,:only=>[:favorite,:remove_favorite,:create_from_flash]
  before_filter :check_administrator_role,:except => [:index, :show,:confirm,:create_from_flash,:favorite,:remove_favorite]
  before_filter :find_cart

  # GET /products
  # GET /products.xml
  def index
    if @pcate.id == DIY_CATE
      @recommend_designs = Design.recommend_list
      sql = "SELECT * FROM products where pcate_id = #{DIY_CATE}"

      if params[:sex]
        sql = "SELECT p.* FROM products p, styles s,materials m
WHERE p.material_id = m.id AND m.style_id = s.id  AND p.pcate_id = #{DIY_CATE} AND s.sex = #{params[:sex]}"
      end

      @products = Product.paginate_by_sql sql,:page => params[:page],:per_page => 12,:order=>"id desc"

      respond_to do |format|
        format.html {render :template => "products/diy_index.html.erb"}
        format.xml  { render :xml => Product.find(:all,:limit=>5).to_xml(:skip_types => true,:dasherize => false,
            :include=>{:prints=>{:except=>[:id,:product_id],
                :methods =>[:x,:y,:front_or_back,:image_url]}},
            :only => [:id],
            :methods =>[:mate_image_url,:mate_back_image_url]) }
      end
    else
      @products = @pcate.products.paginate :page => params[:page],:per_page => 12,:order=>"id desc"
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @products }
      end
    end
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])
    if @pcate.id == DIY_CATE
      @products = [ @product]
      respond_to do |format|
        format.html {render :template=>'products/diy_show.html.erb',:layout=>false}# diy_show.html.erb
        format.xml  { render :xml => @products.to_xml(:skip_types => true,:dasherize => false,
            :include=>{:prints=>{:except=>[:id,:product_id],
                :methods =>[:x,:y,:front_or_back,:image_url]}},
            :only => [:id],
            :methods =>[:mate_image_url,:mate_back_image_url])}
      end
    else
      @pcolor = @product.pcolors.first
      respond_to do |format|
        format.html {render :template=>'pcolors/show.html.erb'}
        format.xml  { render :xml => @product }
      end
    end
  end

  # GET /products/new
  # GET /products/new.xml
  def new
    @product = @pcate.products.build
    @pcate.properties.each do|property|
      @product.holds.build({:property_id=>property.id})
    end
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = @pcate.products.find(params[:id])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = @pcate.products.build(params[:product])
    @product.property_hash = params[:hold] if params[:hold]

    if @product.save
      respond_to do |format|
        flash[:notice] = '商品创建成功.'
        format.html { redirect_to(pcate_products_path(@pcate)) }
        format.xml  { render :xml => @product, :status => :created, :location => @product }
      end
    else
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @product }
      end
    end
  end

  def create_from_flash
    if params[:product][:id]
      @product = Product.find(params[:product][:id])
      if @product.update_attributes(params[:product])
        respond_to do |format|
          format.html { redirect_to('/store/my_cart') }
        end
      end
    else
      @product = Product.check_exist_by_print(params[:product])

      unless @product
        @product = Product.new(params[:product])
        @product.price = @product.mate_price
        @product.pcate_id = DIY_CATE
        @product.save
      end
    
      respond_to do |format|
        if params[:fav] == "1"
          flash[:notice] = '商品创建成功.'
          format.html { redirect_to("/products/favorite/#{@product.id}") }
        else
          flash[:notice] = '商品创建成功,请选择码数.'
          format.html { redirect_to("/products/confirm/#{@product.id}") }
        end
      end
    end
  end

  def confirm
    @product = Product.find(params[:id])
    respond_to do |format|
      format.html # confirm.html.erb
    end
  end

  def favorite
    @product = Product.find(params[:id])
    unless @product.collectors.include?(@user)
      @user.favors << @product
      flash[:notice] = '添加到收藏夹成功.'
    end
    respond_to do |format|
      format.html { redirect_to('/store/my_favorite') }
    end
  end

  def remove_favorite
    @product = Product.find(params[:id])
    if @product.collectors.include?(@user)
      @user.favors.delete(@product)
      flash[:notice] = '更新成功.'
    end
    respond_to do |format|
      format.html { redirect_to('/store/my_favorite') }
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    @product = Product.find(params[:id])
    @product.property_hash = params[:hold] if params[:hold]
    
    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = 'Product was successfully updated.'
        format.html { redirect_to([@pcate,@product]) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(pcate_products_path(@pcate)) }
      format.xml  { head :ok }
    end
  end

  private

  def load_pcate
    @pcate = Pcate.find(params[:pcate_id])
  end

end

